package com.gitee.ice1938.module.admin.app.manage.appdetail;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BaseRepository;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.gitee.ice1938.module.admin.app.manage.appbasetype.AppBaseTypeEntity;

/**
 * RVersionTypeController
 * 
 * 
 * @author ice1938@qq.com 2019-12-2 14:07:10
 *
 */
@Repository
public class RVersionTypeRepository extends BaseRepository {

	@Resource
	private JdbcTemplate jdbcTemplate;

	public int insert(String typeCode, String appPackage, String userLoginName) {
		String sql = "INSERT INTO app_r_version_type (type_code,app_package) VALUES (?,?)";
		int update = jdbcTemplate.update(sql.toString(), typeCode, appPackage);
		return update;
	}

	public int deleteByPackage(String appPackage) {
		StringBuilder sql = new StringBuilder("DELETE FROM app_r_version_type WHERE app_package = ?");
		int update = jdbcTemplate.update(sql.toString(), appPackage);
		return update;
	}

	public int[] batchInsert(String[] data, String appPackage, String userLoginName) {
		String sql = "INSERT INTO app_r_version_type (type_code,app_package) VALUES (?,?)";
		return jdbcTemplate.batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				String typeCode = data[i];
				int index = 1;
				ps.setString(index++, typeCode);
				ps.setString(index++, appPackage);
			}

			@Override
			public int getBatchSize() {
				return data.length;
			}
		});

	}

	public List<AppBaseTypeEntity> findTypeCodeByAppPackage(String appPackage) {
		String sql = "SELECT abt.type_code,abt.type_name from  app_r_version_type r,app_base_type abt where abt.type_code = r.type_code and r.app_package = ?";
		List<AppBaseTypeEntity> content = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(AppBaseTypeEntity.class),
		        appPackage);
		return content;
	}

}
